package IOpearation;

import Book.Book;
import Book.BookList;

import java.util.Arrays;
import java.util.Scanner;

/**
 * Created by xiebang
 * Description
 * User:小谢
 * Date:2021-12-24
 * Time:10:46
 */
public class AddOpearation implements IOPearation{
    @Override
    public void work(BookList bookList) {
        System.out.println("新增书籍!");
        Scanner scanner = new Scanner(System.in);
        //扩容
        if (bookList.getUsedSize() == bookList.getCapacity()){
            bookList.books = Arrays.copyOf(bookList.books,bookList.getCapacity() * 2);
            bookList.setCapacity(bookList.getCapacity() * 2);
        }
        //输入信息
        System.out.println("请输入图书的名字");
        String name = scanner.nextLine();
        System.out.println("请输入图书的作者");
        String author = scanner.nextLine();
        System.out.println("请输入图书的类型");
        String type = scanner.nextLine();
        System.out.println("请输入图书的价格");
        int price = scanner.nextInt();

        Book book = new Book(name,author,price,type);

        int size = bookList.getUsedSize();
        for (int i = 0; i < size; i++) {
            // 判断这本书是否以及存在
            Book book1 = bookList.getBook(i);
            if (book1.getName().equals(name)){
                System.out.println("这本书已经存在");
                break;
            }
        }

        bookList.setBooks(bookList.getUsedSize(),book);
        bookList.setUsedSize(size + 1);
    }

}
